package com.example;

import com.example.observer.Observer;
import com.example.utils.InputHelper;
import com.example.utils.ObserverHelper;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;


/**
 * observer的客户端
 */
public class ObserverConsumer {
    public static void main(String[] args) throws RemoteException, NotBoundException {

        Machine machine = new Machine(5);

        // 获取provider注册的那个observer
        ObserverHelper observerHelper = new ObserverHelper();
        // ObserverHelper observerHelper = new ObserverHelper("192.168.100.131", ObserverHelper.DEFAULT_PORT);
        Observer observer = observerHelper.getRegisteredObserver("observer");

        System.out.println("获取observer成功");

        machine.attach(observer);
        System.out.println("添加observer成功");

        ///// 测试开始
        InputHelper.getLine("按下回车键进行测试...");

        machine.insertQuarter();
        machine.turnCrank();
        machine.dispense();

        machine.insertQuarter();
        machine.turnCrank();
        machine.dispense();

        machine.insertQuarter();
        machine.turnCrank();
        machine.dispense();

        ////// 移除 observer
        InputHelper.getLine("按下回车键移除observer...");
        machine.detach(observer);

        System.out.println("成功移除observer");
        machine.insertQuarter();
        machine.turnCrank();
        machine.dispense();

        System.out.println("machine.getCount() = " + machine.getCount());
    }
}

